<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斜面运动物理模拟</title>
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4cc9f0;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4ade80;
            --warning: #f97316;
            --danger: #ef4444;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%);
            min-height: 100vh;
            padding: 20px;
            color: var(--dark);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            display: grid;
            grid-template-columns: 1fr;
            gap: 25px;
        }

        @media (min-width: 768px) {
            .container {
                grid-template-columns: 1fr 1fr;
            }
        }

        .card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
            padding: 25px;
            transition: transform 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
        }

        header {
            text-align: center;
            margin-bottom: 30px;
        }

        h1 {
            color: var(--primary);
            font-size: 2.2rem;
            margin-bottom: 10px;
            background: linear-gradient(to right, var(--primary), var(--accent));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .subtitle {
            color: #6c757d;
            font-size: 1.1rem;
            max-width: 700px;
            margin: 0 auto;
            line-height: 1.6;
        }

        h2 {
            color: var(--secondary);
            font-size: 1.5rem;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 2px solid var(--accent);
            display: inline-block;
        }

        .simulation-container {
            position: relative;
            height: 350px;
            background: #f0f8ff;
            border-radius: 12px;
            overflow: hidden;
            border: 1px solid #e0e7ff;
        }

        #animationCanvas {
            width: 100%;
            height: 100%;
        }

        .controls {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-top: 20px;
        }

        .control-group {
            display: flex;
            flex-direction: column;
        }

        label {
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--secondary);
            font-size: 0.9rem;
        }

        input[type="range"] {
            width: 100%;
            height: 8px;
            border-radius: 4px;
            background: #e0e7ff;
            outline: none;
            -webkit-appearance: none;
        }

        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: var(--primary);
            cursor: pointer;
            box-shadow: 0 2px 6px rgba(67, 97, 238, 0.4);
        }

        .data-panel {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 15px;
            margin-top: 20px;
            background: #f8f9ff;
            padding: 15px;
            border-radius: 12px;
        }

        .data-item {
            text-align: center;
            padding: 12px;
            border-radius: 8px;
            background: white;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }

        .data-label {
            font-size: 0.9rem;
            color: #6c757d;
            margin-bottom: 5px;
        }

        .data-value {
            font-size: 1.4rem;
            font-weight: 700;
            color: var(--primary);
        }

        .unit {
            font-size: 0.8rem;
            color: #6c757d;
            margin-left: 2px;
        }

        .chart-container {
            height: 300px;
            position: relative;
        }

        .physics-concepts {
            margin-top: 25px;
        }

        .concept-list {
            padding-left: 20px;
        }

        .concept-item {
            margin-bottom: 15px;
            line-height: 1.6;
        }

        .concept-title {
            font-weight: 600;
            color: var(--secondary);
        }

        .formula {
            font-family: 'Courier New', monospace;
            background: #f0f8ff;
            padding: 8px 12px;
            border-radius: 6px;
            display: inline-block;
            margin: 8px 0;
            color: var(--primary);
            font-weight: 600;
        }

        .buttons {
            display: flex;
            gap: 12px;
            margin-top: 20px;
        }

        .btn {
            padding: 14px 24px;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 1rem;
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-secondary {
            background: #e0e7ff;
            color: var(--primary);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(0,0,0,0.1);
        }

        .btn:active {
            transform: translateY(0);
        }

        footer {
            text-align: center;
            margin-top: 40px;
            color: #6c757d;
            font-size: 0.9rem;
            padding: 20px;
        }

        .param-value {
            text-align: right;
            font-weight: 600;
            color: var(--primary);
            font-size: 0.95rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>斜面运动物理模拟实验</h1>
            <p class="subtitle">本实验演示了小车在斜面上的匀加速直线运动，实时显示运动参数和物理量变化曲线</p>
        </header>

        <div class="card">
            <h2>斜面运动模拟</h2>
            <div class="simulation-container">
                <canvas id="animationCanvas"></canvas>
            </div>

            <div class="controls">
                <div class="control-group">
                    <label for="angleControl">斜面角度: <span class="param-value" id="angleValue">30°</span></label>
                    <input type="range" id="angleControl" min="10" max="80" value="30">
                </div>

                <div class="control-group">
                    <label for="massControl">小车质量: <span class="param-value" id="massValue">2.0 kg</span></label>
                    <input type="range" id="massControl" min="0.5" max="5" step="0.1" value="2.0">
                </div>

                <div class="control-group">
                    <label for="frictionControl">摩擦系数: <span class="param-value" id="frictionValue">0.1</span></label>
                    <input type="range" id="frictionControl" min="0" max="0.5" step="0.01" value="0.1">
                </div>
            </div>

            <div class="data-panel">
                <div class="data-item">
                    <div class="data-label">当前速度</div>
                    <div class="data-value" id="velocity">0.00 <span class="unit">m/s</span></div>
                </div>
                <div class="data-item">
                    <div class="data-label">位移</div>
                    <div class="data-value" id="displacement">0.00 <span class="unit">m</span></div>
                </div>
                <div class="data-item">
                    <div class="data-label">加速度</div>
                    <div class="data-value" id="acceleration">0.00 <span class="unit">m/s²</span></div>
                </div>
                <div class="data-item">
                    <div class="data-label">时间</div>
                    <div class="data-value" id="time">0.00 <span class="unit">s</span></div>
                </div>
            </div>

            <div class="buttons">
                <button id="startBtn" class="btn btn-primary">
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z"/>
                    </svg>
                    开始运动
                </button>
                <button id="resetBtn" class="btn btn-secondary">
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"/>
                        <path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"/>
                    </svg>
                    重置实验
                </button>
            </div>
        </div>

        <div class="card">
            <h2>运动关系曲线</h2>
            <div class="chart-container">
                <canvas id="velocityChart"></canvas>
            </div>
            <div class="chart-container" style="margin-top: 30px;">
                <canvas id="displacementChart"></canvas>
            </div>
        </div>

        <div class="card">
            <h2>物理原理</h2>
            <div class="physics-concepts">
                <ul class="concept-list">
                    <li class="concept-item">
                        <div class="concept-title">牛顿第二定律</div>
                        <p>物体加速度与所受合力成正比：<span class="formula">F = m·a</span></p>
                    </li>
                    <li class="concept-item">
                        <div class="concept-title">斜面受力分析</div>
                        <p>重力分量提供加速度：<span class="formula">a = g·sinθ - μg·cosθ</span></p>
                        <p>其中θ为斜面角度，μ为摩擦系数，g为重力加速度(9.8 m/s²)</p>
                    </li>
                    <li class="concept-item">
                        <div class="concept-title">匀加速运动公式</div>
                        <p>速度公式：<span class="formula">v = v₀ + at</span></p>
                        <p>位移公式：<span class="formula">s = v₀t + ½at²</span></p>
                    </li>
                    <li class="concept-item">
                        <div class="concept-title">能量守恒</div>
                        <p>重力势能转化为动能：<span class="formula">mgh = ½mv² + W<sub>f</sub></span></p>
                        <p>其中W<sub>f</sub>为克服摩擦力做功</p>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <footer>
        <p>斜面运动物理模拟实验 | 展示匀加速直线运动规律 | 可用于物理教学演示</p>
    </footer>

    <script>
        // 全局变量
        const canvas = document.getElementById('animationCanvas');
        const ctx = canvas.getContext('2d');
        let animationId;
        let startTime;
        let elapsedTime = 0;
        let isAnimating = false;

        // 物理参数
        let angle = 30; // 斜面角度（度）
        let mass = 2.0; // 小车质量（kg）
        let friction = 0.1; // 摩擦系数
        const gravity = 9.8; // 重力加速度（m/s²）

        // 小车参数
        const carWidth = 50;
        const carHeight = 30;
        let carPosition = { x: 100, y: 0 };
        let carVelocity = 0;
        let carAcceleration = 0;
        let displacement = 0;

        // 初始化函数
        function init() {
            resizeCanvas();
            drawScene();
            setupEventListeners();
            updateParamsDisplay();
        }

        // 调整Canvas大小
        function resizeCanvas() {
            const container = canvas.parentElement;
            canvas.width = container.clientWidth;
            canvas.height = container.clientHeight;

            // 初始小车位置
            carPosition = {
                x: canvas.width * 0.15,
                y: canvas.height * 0.7 - carHeight
            };
        }

        // 绘制场景
        function drawScene() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 绘制斜面
            const inclineLength = canvas.width * 0.7;
            const inclineHeight = inclineLength * Math.sin(angle * Math.PI / 180);
            const startX = canvas.width * 0.15;
            const startY = canvas.height * 0.7;
            const endX = startX + inclineLength * Math.cos(angle * Math.PI / 180);
            const endY = startY - inclineHeight;

            // 斜面
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(endX, endY);
            ctx.lineWidth = 8;
            ctx.strokeStyle = '#94a3b8';
            ctx.stroke();

            // 绘制地面
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(canvas.width, startY);
            ctx.lineWidth = 3;
            ctx.strokeStyle = '#64748b';
            ctx.stroke();

            // 绘制小车
            drawCar(carPosition.x, carPosition.y);

            // 绘制角度标注
            drawAngleIndicator(startX, startY, angle);
        }

        // 绘制小车
        function drawCar(x, y) {
            ctx.save();
            ctx.translate(x, y);

            // 小车主体
            ctx.fillStyle = '#3b82f6';
            ctx.fillRect(0, 0, carWidth, carHeight);

            // 车窗
            ctx.fillStyle = '#bfdbfe';
            ctx.fillRect(carWidth * 0.7, carHeight * 0.2, carWidth * 0.2, carHeight * 0.4);

            // 车轮
            ctx.fillStyle = '#1e293b';
            ctx.beginPath();
            ctx.arc(carWidth * 0.2, carHeight + 8, 8, 0, Math.PI * 2);
            ctx.fill();
            ctx.beginPath();
            ctx.arc(carWidth * 0.8, carHeight + 8, 8, 0, Math.PI * 2);
            ctx.fill();

            ctx.restore();
        }

        // 绘制角度指示器
        function drawAngleIndicator(x, y, angle) {
            const radius = 40;
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, Math.PI * 2);
            ctx.strokeStyle = 'rgba(100, 100, 100, 0.2)';
            ctx.lineWidth = 1;
            ctx.stroke();

            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(x + radius, y);
            ctx.moveTo(x, y);

            const radian = angle * Math.PI / 180;
            ctx.lineTo(x + radius * Math.cos(radian), y - radius * Math.sin(radian));
            ctx.strokeStyle = '#ef4444';
            ctx.lineWidth = 2;
            ctx.stroke();

            // 角度文本
            ctx.font = 'bold 16px Arial';
            ctx.fillStyle = '#ef4444';
            ctx.fillText(`${angle}°`, x + radius * 0.5 + 10, y - radius * 0.5 - 10);
        }

        // 更新物理参数显示
        function updateParamsDisplay() {
            document.getElementById('angleValue').textContent = angle + '°';
            document.getElementById('massValue').textContent = mass.toFixed(1) + ' kg';
            document.getElementById('frictionValue').textContent = friction.toFixed(2);
        }

        // 设置事件监听器
        function setupEventListeners() {
            window.addEventListener('resize', () => {
                resizeCanvas();
                if (!isAnimating) drawScene();
            });

            // 滑块控制
            document.getElementById('angleControl').addEventListener('input', function() {
                angle = parseInt(this.value);
                updateParamsDisplay();
                if (!isAnimating) {
                    resetSimulation();
                    drawScene();
                }
            });

            document.getElementById('massControl').addEventListener('input', function() {
                mass = parseFloat(this.value);
                updateParamsDisplay();
            });

            document.getElementById('frictionControl').addEventListener('input', function() {
                friction = parseFloat(this.value);
                updateParamsDisplay();
            });

            // 按钮控制
            document.getElementById('startBtn').addEventListener('click', toggleAnimation);
            document.getElementById('resetBtn').addEventListener('click', resetSimulation);
        }

        // 切换动画状态
        function toggleAnimation() {
            if (isAnimating) {
                pauseAnimation();
                document.getElementById('startBtn').innerHTML = `
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z"/>
                    </svg>
                    开始运动
                `;
            } else {
                startAnimation();
                document.getElementById('startBtn').innerHTML = `
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M5.5 3.5A1.5 1.5 0 0 1 7 5v6a1.5 1.5 0 0 1-3 0V5a1.5 1.5 0 0 1 1.5-1.5zm5 0A1.5 1.5 0 0 1 12 5v6a1.5 1.5 0 0 1-3 0V5a1.5 1.5 0 0 1 1.5-1.5z"/>
                    </svg>
                    暂停运动
                `;
            }
        }

        // 开始动画
        function startAnimation() {
            if (!isAnimating) {
                isAnimating = true;
                startTime = Date.now() - elapsedTime;
                animate();
            }
        }

        // 暂停动画
        function pauseAnimation() {
            if (isAnimating) {
                isAnimating = false;
                cancelAnimationFrame(animationId);
            }
        }

        // 重置模拟
        function resetSimulation() {
            pauseAnimation();
            elapsedTime = 0;
            carVelocity = 0;
            displacement = 0;
            updateDataDisplay();
            resizeCanvas();
            drawScene();
            document.getElementById('startBtn').innerHTML = `
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z"/>
                </svg>
                开始运动
            `;
        }

        // 动画循环
        function animate() {
            if (!isAnimating) return;

            const currentTime = Date.now();
            elapsedTime = currentTime - startTime;
            const deltaTime = 0.05; // 时间增量（秒）

            // 计算加速度 (a = g*sinθ - μ*g*cosθ)
            const radian = angle * Math.PI / 180;
            carAcceleration = gravity * Math.sin(radian) - friction * gravity * Math.cos(radian);

            // 更新速度和位移
            carVelocity += carAcceleration * deltaTime;
            displacement += carVelocity * deltaTime + 0.5 * carAcceleration * deltaTime * deltaTime;

            // 更新小车位置            const inclineLength = canvas.width * 0.7;
            const maxDisplacement = inclineLength - carWidth;
            const displacementRatio = Math.min(displacement / maxDisplacement, 1);

            const startX = canvas.width * 0.15;
            const startY = canvas.height * 0.7;
            carPosition.x = startX + displacementRatio * inclineLength * Math.cos(radian);
            carPosition.y = startY - carHeight - displacementRatio * inclineLength * Math.sin(radian);

            // 绘制场景
            drawScene();

            // 更新数据显示
            updateDataDisplay();

            // 到达底部停止
            if (displacementRatio >= 1) {
                pauseAnimation();
                document.getElementById('startBtn').innerHTML = `
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z"/>
                    </svg>
                    重新开始
                `;
            } else {
                animationId = requestAnimationFrame(animate);
            }
        }

        // 更新数据显示
        function updateDataDisplay() {
            document.getElementById('velocity').innerHTML = carVelocity.toFixed(2) + ' <span class="unit">m/s</span>';
            document.getElementById('displacement').innerHTML = displacement.toFixed(2) + ' <span class="unit">m</span>';
            document.getElementById('acceleration').innerHTML = carAcceleration.toFixed(2) + ' <span class="unit">m/s²</span>';
            document.getElementById('time').innerHTML = (elapsedTime / 1000).toFixed(2) + ' <span class="unit">s</span>';
        }

        // 初始化图表示例（实际应用中应使用图表库如Chart.js）
        function initCharts() {
            const vCtx = document.getElementById('velocityChart').getContext('2d');
            const dCtx = document.getElementById('displacementChart').getContext('2d');

            // 速度图表
            drawChart(vCtx, "速度-时间关系图", "v = v₀ + a·t", [
                {x: 0, y: 0}, {x: 1, y: 1.96}, {x: 2, y: 3.92},
                {x: 3, y: 5.88}, {x: 4, y: 7.84}, {x: 5, y: 9.8}
            ], '#ef4444');

            // 位移图表
            drawChart(dCtx, "位移-时间关系图", "s = v₀·t + ½a·t²", [
                {x: 0, y: 0}, {x: 1, y: 0.98}, {x: 2, y: 3.92},
                {x: 3, y: 8.82}, {x: 4, y: 15.68}, {x: 5, y: 24.5}
            ], '#3b82f6');
        }

        // 绘制简单图表
        function drawChart(ctx, title, formula, data, color) {
            const width = ctx.canvas.width;
            const height = ctx.canvas.height;
            const padding = 40;

            // 清除画布
            ctx.clearRect(0, 0, width, height);

            // 设置样式
            ctx.font = '14px Arial';
            ctx.fillStyle = '#64748b';
            ctx.textAlign = 'center';

            // 绘制标题
            ctx.font = 'bold 16px Arial';
            ctx.fillStyle = '#1e293b';
            ctx.fillText(title, width / 2, padding - 15);

            // 绘制公式
            ctx.font = 'italic 14px Arial';
            ctx.fillStyle = '#64748b';
            ctx.fillText(formula, width / 2, padding - 15 + 20);

            // 计算缩放比例
            const maxX = Math.max(...data.map(p => p.x));
            const maxY = Math.max(...data.map(p => p.y));
            const scaleX = (width - 2 * padding) / maxX;
            const scaleY = (height - 2 * padding) / (maxY || 1);

            // 绘制坐标轴
            ctx.strokeStyle = '#94a3b8';
            ctx.lineWidth = 2;

            // X轴
            ctx.beginPath();
            ctx.moveTo(padding, height - padding);
            ctx.lineTo(width - padding, height - padding);
            ctx.stroke();

            // Y轴
            ctx.beginPath();
            ctx.moveTo(padding, padding);
            ctx.lineTo(padding, height - padding);
            ctx.stroke();

            // 绘制网格            ctx.strokeStyle = '#e2e8f0';
            ctx.lineWidth = 1;

            // 垂直网格线
            for (let i = 1; i < maxX; i++) {
                const x = padding + i * scaleX;
                ctx.beginPath();
                ctx.moveTo(x, padding);
                ctx.lineTo(x, height - padding);
                ctx.stroke();

                // X轴刻度
                ctx.fillStyle = '#64748b';
                ctx.fillText(i.toString(), x, height - padding + 20);
            }

            // 水平网格线
            for (let i = 1; i <= 5; i++) {
                const yValue = maxY * i / 5;
                const y = height - padding - yValue * scaleY;

                ctx.beginPath();
                ctx.moveTo(padding, y);
                ctx.lineTo(width - padding, y);
                ctx.stroke();

                // Y轴刻度
                ctx.fillStyle = '#64748b';
                ctx.textAlign = 'right';
                ctx.fillText(yValue.toFixed(1), padding - 10, y + 5);
            }

            // 轴标签
            ctx.textAlign = 'center';
            ctx.fillText('时间 (s)', width / 2, height - padding + 35);

            ctx.save();
            ctx.translate(15, height / 2);
            ctx.rotate(-Math.PI / 2);
            ctx.textAlign = 'center';
            ctx.fillText(title.includes('速度') ? '速度 (m/s)' : '位移 (m)', 0, 0);
            ctx.restore();

            // 绘制数据线
            ctx.beginPath();
            ctx.strokeStyle = color;
            ctx.lineWidth = 3;
            ctx.lineJoin = 'round';

            data.forEach((point, i) => {
                const x = padding + point.x * scaleX;
                const y = height - padding - point.y * scaleY;

                if (i === 0) {
                    ctx.moveTo(x, y);
                } else {
                    ctx.lineTo(x, y);
                }
            });

            ctx.stroke();

            // 绘制数据点
            data.forEach(point => {
                const x = padding + point.x * scaleX;
                const y = height - padding - point.y * scaleY;

                ctx.beginPath();
                ctx.arc(x, y, 5, 0, Math.PI * 2);
                ctx.fillStyle = color;
                ctx.fill();
                ctx.strokeStyle = 'white';
                ctx.lineWidth = 2;
                ctx.stroke();
            });
        }

        // 页面加载完成后初始化
        window.onload = function() {
            init();
            initCharts();
        };
    </script>
</body>
</html>